<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo-box">
        <img src="@/assets/weblogo.png" alt="" width="200">
      </div>
      <el-form ref='loginFormRef' :model="formLabelAlign">
        <el-form-item>
          <el-input prefix-icon="el-icon-user-solid" v-model="formLabelAlign.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input prefix-icon="el-icon-lock" type="password" v-model="formLabelAlign.password" @keyup.enter.native="goLogin"></el-input>
        </el-form-item>
        <el-form-item class="submit">
          <el-button @click="goLogin" type="success">登录</el-button>
          <el-button @click="reset" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginCom',
  data() {
    return {
      formLabelAlign: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    async goLogin() {
      const { data: rel } = await this.$http.post('login', this.formLabelAlign)
      if (rel.code === 20000 && rel.token) {
        // 存储token
        localStorage.setItem('token', rel.token)
        localStorage.setItem('username', this.formLabelAlign.username)
        localStorage.setItem('userrole', rel.role)
        this.$message({
          message: '登录成功',
          type: 'success'
        })
        this.$router.push('/home')
      } else {
        this.$message.error(rel.message)
      }
    },
    // 重置表单
    reset() {
      this.formLabelAlign.username = ''
      this.formLabelAlign.password = ''
    }
  }
}
</script>

<style scoped>
.login-container {
  background-color: #6a9d03;
  height: 100%;
  position: relative;
}
.login-box {
  width: 500px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
}
.logo-box {
  position: absolute;
  left: 50%;
  top: -80px;
  border: 1px solid #eeeeee;
  border-radius: 50%;
  transform: translateX(-50%);
  box-shadow: 0 0 10px #ddd;
}
.logo-box img {
  background: #eeeeee;
  border-radius: 50%;
  border: 20px solid #fff;
}
.el-form-item {
  width: 90%;
  margin: 30px auto;
}
.el-form {
  margin-top: 100px;
}
.submit {
  display: flex;
  justify-content: flex-end;
}
</style>
